package stream;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * Date:2022/2/21
 * Author:ybc
 * Description:
 * 创建Stream方式:
 * 1、通过集合创建
 * 集合.stream()
 * 2、通过数组创建
 * Arrays.stream(arr)
 * 3、通过Stream中的静态方法创建
 * Stream.of(T... values)
 * 4、创建无限流
 * Stream.generate(Supplier<T> s)
 *
 * 常用的中间操作：
 * limit(long maxSize)：获取Stream流中最多maxSize个数据
 * skip(long n)：获取Stream流中n+1之后的数据
 * filter(Predicate predicate)：筛选Stream中符合条件的数据
 * map(Function<? super T, ? extends R> mapper)：将数据流中的数据转换为另一个数据
 * Stream.concat(stream1,stream2)：将多个数据流合并为一个
 * 常用的终结操作：
 * forEach(Consumer consumer)：遍历数据流中的数据
 * count()：获取数据流中数据的数量
 */
public class StreamTest {

    @Test
    public void testCreateStream(){
        //创建Stream方式1：通过集合创建
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 6);
        Stream<Integer> stream1 = list.stream();
        stream1.limit(3).forEach(s-> System.out.println(s));
        System.out.println("+++++++++++++++++++++++++++++++++++++++++");
        String[] strArr = new String[]{"a","b","c","d","e"};
        Stream<String> stream2 = Arrays.stream(strArr);
        stream2.skip(2).forEach(s-> System.out.println(s));
        System.out.println("+++++++++++++++++++++++++++++++++++++++++");
        Stream<Integer> stream3 = Stream.of(1, 2, 3, 4, 5, 6);
        System.out.println(stream3.filter(i -> i % 2 == 0).map(s -> s + 10).count());
        System.out.println("+++++++++++++++++++++++++++++++++++++++++");
        //Stream.generate(()->Math.random()).forEach(s-> System.out.println(s));

    }

}
